﻿namespace Mic.Identity.Service
{
    public class LazyTestService : ILazyTestService
    {
        public ExpensiveObject ExpensiveObject => lazyExpensiveObject.Value;
        // 使用 Lazy<T> 创建一个延迟加载的对象
        private readonly Lazy<ExpensiveObject> lazyExpensiveObject;

        public LazyTestService()
        {
            lazyExpensiveObject = new Lazy<ExpensiveObject>(() => new ExpensiveObject(), true);
        }

        public void LazyTest()
        {
            var w = ExpensiveObject;
        }
    }

    public class ExpensiveObject
    {
        public ExpensiveObject()
        {
            // 模拟一个初始化代价很昂贵的操作
            Console.WriteLine("Expensive object initialized.");
        }

        public string SomeProperty { get; set; }
    }
}
